local pizhi = fk.CreateSkill{
  name = "hanqing__pizhi",
}

Fk:loadTranslationTable{
  ["hanqing__pizhi"] = "睥峙",

  [":hanqing__pizhi"] = "每轮开始时，你可以亮出牌堆顶X张牌（X为存活角色数），令所有角色依次选择一项：1.横置并获得其中一张；2.令你获得其中一张牌"..
  "并交给其一张牌。",
  ["#hanqing__pizhi-prey"] = "睥峙：获得其中一张牌",
  ["hanqing__pizhi1"] = "横置并获得一张牌",
  ["hanqing__pizhi2"] = "%src获得一张牌并交给你一张牌",
  ["#hanqing__pizhi-give"] = "睥峙：请交给 %dest 一张牌",

  --[":hanqing__pizhi"] = "准备阶段，你可以展示所有其他角色各一张手牌并令这些角色依次选择一项：1.将此牌交给你；2.横置并获得另一名角色展示的牌。",
  --["#hanqing__pizhi-show"] = "睥峙：展示 %dest 一张手牌",
  --["#hanqing__pizhi-choice"] = "睥峙：请选择一项",
  --["@hanqing__pizhi-tmp"] = "",
  --["hanqing__pizhi1"] = "将你展示的牌交给 %src",
  --["hanqing__pizhi2"] = "横置并获得另一名角色的展示牌",
  --["#hanqing__pizhi-prey"] = "睥峙：选择一名角色获得其展示牌",
}

pizhi:addEffect(fk.RoundStart, {
  anim_type = "drawcard",
  events = {fk.RoundStart},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(pizhi.name)
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    if room:askToSkillInvoke(player, {
      skill_name = pizhi.name,
    }) then
      event:setCostData(self, {tos = room:getAlivePlayers()})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = room:getNCards(#room.alive_players)
    room:turnOverCardsFromDrawPile(player, cards, pizhi.name)
    room:delay(2000)

    for _, p in ipairs(room:getAlivePlayers()) do
      cards = table.filter(cards, function (id)
        return room:getCardArea(id) == Card.Processing
      end)
      if #cards == 0 then return end
      if not p.dead then
        local choices = {"hanqing__pizhi2:"..player.id}
        if not p.chained then
          table.insert(choices, 1, "hanqing__pizhi1")
        end
        local choice = room:askToViewCardsAndChoice(p, {
          cards = cards,
          choices = choices,
          skill_name = pizhi.name,
          prompt = "#hanqing__pizhi-choice",
        })
        if choice == "hanqing__pizhi1" then
          local card = room:askToChooseCard(p, {
            target = p,
            flag = { card_data = {{ pizhi.name, cards }} },
            skill_name = pizhi.name,
            prompt = "#hanqing__pizhi-prey",
          })
          p:setChainState(true)
          if not p.dead then
            room:moveCardTo(card, Card.PlayerHand, p, fk.ReasonJustMove, pizhi.name, nil, true, p)
          end
        elseif not player.dead then
          local card = room:askToChooseCard(player, {
            target = player,
            flag = { card_data = {{ pizhi.name, cards }} },
            skill_name = pizhi.name,
            prompt = "#hanqing__pizhi-prey",
          })
          room:moveCardTo(card, Card.PlayerHand, player, fk.ReasonJustMove, pizhi.name, nil, true, player)
          if p ~= player and not player.dead and not player:isNude() and not p.dead then
            card = room:askToCards(player, {
              min_num = 1,
              max_num = 1,
              include_equip = true,
              skill_name = pizhi.name,
              prompt = "#hanqing__pizhi-give::"..p.id,
              cancelable = false,
            })[1]
            room:moveCardTo(card, Card.PlayerHand, p, fk.ReasonGive, pizhi.name, nil, false, player)
          end
        end
      end
    end
    room:cleanProcessingArea(cards)
  end,
})

--[[
pizhi:addEffect(fk.EventPhaseStart, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(pizhi.name) and player.phase == Player.Start and
      table.find(player.room:getOtherPlayers(player, false), function (p)
        return not p:isKongcheng()
      end)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if room:askToSkillInvoke(player, {
      skill_name = pizhi.name,
    }) then
      local tos = table.filter(room:getOtherPlayers(player), function(p)
        return not p:isKongcheng()
      end)
      event:setCostData(self, {tos = tos})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local targets = event:getCostData(self).tos
    local dat = {}
    for _, p in ipairs(targets) do
      if player.dead then return end
      if not p.dead and not p:isKongcheng() then
        local card = room:askToChooseCard(player, {
          target = p,
          flag = "h",
          skill_name = pizhi.name,
          prompt = "#hanqing__pizhi-show::"..p.id,
        })
        dat[p] = card
        p:showCards(card)
      end
    end

    for _, p in ipairs(targets) do
      if not p.dead then
        local choices = {}
        if not player.dead and table.contains(p:getCardIds("h"), dat[p]) then
          table.insert(choices, "hanqing__pizhi1:"..player.id)
        end
        if not p.chained then
          for _, q in ipairs(targets) do
            if not q.dead and table.contains(q:getCardIds("h"), dat[q]) then
              if q ~= p then
                table.insertIfNeed(choices, "hanqing__pizhi2")
              end
              room:setPlayerMark(q, "@hanqing__pizhi-tmp", Fk:getCardById(dat[q]):toLogString())
            end
          end
        end
        if #choices > 0 then
          local choice = room:askToChoice(p, {
            choices = choices,
            skill_name = pizhi.name,
            prompt = "#hanqing__pizhi-choice",
          })
          if choice == "hanqing__pizhi2" then
            local tos = table.filter(targets, function (q)
              return q ~= p and table.contains(q:getCardIds("h"), dat[q])
            end)
            local to = room:askToChoosePlayers(player, {
              min_num = 1,
              max_num = 1,
              targets = tos,
              skill_name = pizhi.name,
              prompt = "#hanqing__pizhi-prey",
              cancelable = false,
            })[1]
            p:setChainState(true)
            if not p.dead and not to.dead and table.contains(to:getCardIds("h"), dat[to]) then
              room:moveCardTo(dat[to], Card.PlayerHand, p, fk.ReasonPrey, pizhi.name, nil, true, p)
            end
          else
            room:moveCardTo(dat[p], Card.PlayerHand, player, fk.ReasonGive, pizhi.name, nil, true, p)
          end
        end
        for _, q in ipairs(targets) do
          room:setPlayerMark(q, "@hanqing__pizhi-tmp", 0)
        end
      end
    end
  end,
})
]]

return pizhi
